﻿using ComLib.ComType;
using EECMISWS;
using KWindow.KPageCore;
using LogLib;
using PhotoNamingService;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IPCPages
{
    public partial class PageSelectPeople : PageControlBase
    {
        /// <summary>
        /// 连续点击激活次数统计
        /// </summary>
        private int active_click_count = 0;
        private Stopwatch sw = new Stopwatch();
        private PictureBox pictureBox1 = null;
        private PictureBox pictureBoxBackground = null;
        private Button buttonStart = null;
        private Button buttonChinese = null;
        private Button buttonForeigner = null;
        private Button buttonOther = null;

        public PageSelectPeople()
        {          
            InitializeComponent();
            SetProcessBase(ProcessSelectPeople.Instance);
        }

        private void GetControls()
        {
            labelTitle = GetControl<Label>("labelTitle") as Label;
            pictureBox1 = GetControl<PictureBox>("pictureBox1") as PictureBox;
            pictureBoxBackground = GetControl<PictureBox>("pictureBoxBackground") as PictureBox;
            buttonStart = GetControl<Button>("buttonStart") as Button;
            buttonChinese = GetControl<Button>("buttonChinese") as Button;
            buttonForeigner = GetControl<Button>("buttonForeigner") as Button;
            buttonOther = GetControl<Button>("buttonOther") as Button;
            SetButtonDefaultStyles(buttonStart);
            SetButtonDefaultStyles(buttonChinese);
            SetButtonDefaultStyles(buttonForeigner);
            SetButtonDefaultStyles(buttonOther);
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();
            //this.Controls.Clear();
            //if (null != pictureBox1)
            //    pictureBox1.Parent = pictureBoxBackground;

            //if (null != buttonStart)
            //    buttonStart.Parent = pictureBoxBackground;

            //buttonChinese.Parent = pictureBoxBackground;
        }

        //初始化窗体数据
        public override void InitData()
        {
            SD.ClearValue();
            Process.SetValue("ActionIndex", 1);
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            WebDataAccess webAccess = WebDataAccess.Instance;
            if (webAccess.Enabled && !webAccess.Inited)
            {
                Task.Factory.StartNew(() =>
                {
                    if (!webAccess.Login())
                    {
                        string strLog = "系统登录失败:" + webAccess.ErrorMessage;
                        Log.WriteAsync(strLog);
                        ShowLoginFailedMessage(webAccess.ErrorMessage);
                    }
                });
            }            
        }

        private void ShowLoginFailedMessage(string error)
        {
            Action<string> handler = (e) =>
            {
                MessageBox.Show(e, "全国版系统登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };
            this.Invoke(handler, error);
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            Process.ToNext();
        }

        private void buttonChinese_Click(object sender, EventArgs e)
        {
            if (ProductConfig.Instance.TakePhotoType=="0") //只拍出入境
            {
                Process.ToAny("IPCPages.PageReadIDCard");
            }
            else
            {
                Process.ToAny("IPCPages.PageSelectApply");
            }                
        }

        private void buttonForeigner_Click(object sender, EventArgs e)
        {
            Process.SetValue("ApplyTo", ApplyTo.PortVisa);
            Process.ToAny("IPCPages.PageReadPassport");
        }

        private void buttonOther_Click(object sender, EventArgs e)
        {
            Process.SetValue("ApplyTo", ApplyTo.PortVisa);
            Process.ToAny("IPCPages.PageReadPassport");
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (!sw.IsRunning)
            {
                sw.Restart();
                return;
            }
            if (sw.ElapsedMilliseconds > 2000)
            {
                sw.Reset();
                active_click_count = 0;
            }
            else
            {
                active_click_count++;
            }
            if (active_click_count == 2)
            {
                sw.Reset();
                active_click_count = 0;
                Log.WriteAsync("用户打开了管理员选项");
                FormAdministrator form = new FormAdministrator();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    Log.WriteAsync("即将执行管理员选项动作:" + form.SelectAction.ToString());
                    Process.OnTakeAdminAction(form.SelectAction);
                }
            }
        }
    }
}
